from flask import Blueprint, request, jsonify
from flask_login import current_user
from models import User
import logging

logger = logging.getLogger('Personal_Blog_System')

# 创建用户API蓝图
user_api = Blueprint('user_api', __name__, url_prefix='/api/users')

@user_api.route('/current', methods=['GET'])
def get_current_user():
    """获取当前用户信息"""
    try:
        if current_user.is_authenticated:
            return jsonify({
                'success': True,
                'user': {
                    'id': current_user.id,
                    'username': current_user.username,
                    'name': current_user.name,
                    'email': current_user.email,
                    'avatar': current_user.avatar,
                    'bio': current_user.bio,
                    'role': current_user.role,
                    'created_at': current_user.created_at.isoformat()
                }
            })
        else:
            return jsonify({'success': False, 'message': '用户未登录'}), 401
    except Exception as e:
        logger.error(f"获取当前用户信息失败: {str(e)}")
        return jsonify({'success': False, 'message': '获取用户信息失败'}), 500

